<?php

class StructureController extends Crb_Controller
{

    public function preDispatch()
    {
        parent::preDispatch();
        $this->view->layout()->setLayout('admin');
    }

    public function indexAction()
    {
        $this->view->headScript()->appendFile($this->view->baseUrl('modules/structure/structure.js'));
        $this->view->headLink()->appendStylesheet($this->view->baseUrl('stuff/jstree/themes/default/style.css'));
        
        $this->view->layout()->setLayout('admin');
    }


    public function operationsAction()
    {
        $this->setNoRender();
        $this->disableLayout();

        $Request = $this->getRequest();

        $operation = $Request->getParam('operation');

        $structureDAO = new Structure();
        $Response = array('status'=>1,'description' => '');
        try {
            switch ($operation)
            {
                case 'get_children':
                    echo $this->_helper->json($structureDAO->buildJsTree( $structureDAO->buildHierarchy() ));
                    break;
                case 'create_node':
                    $position = $Request->getParam('id');
                    $title = $Request->getParam('title');
                    $relPosition = $Request->getParam('position');
                    $newID = $structureDAO->add($position, $title);
                    $structureDAO->setPosition($newID, $relPosition);

                    $Response['id'] = $newID;
                    break;
                case 'rename_node':
                    $position = $Request->getParam('id');
                    $title = $Request->getParam('title');
                    $structureDAO->rename($position, $title);
                    break;
                case 'remove_node':
                    $id = $Request->getParam('id');
                    $structureDAO->delete($id);
                    break;
                case 'move_node':
                    $copy     = $Request->getParam('copy',0);
                    $parentId = $Request->getParam('ref',0);
                    $position = $Request->getParam('position',0);
                    $id       = $Request->getParam('id');

                    $structureDAO->move($id,$parentId);
                    $structureDAO->setPosition($id, $position);
                    break;
            }
        } catch (Exception $e) {
            $Response['description'] = $e->getMessage();
            $Response['status'] = 0;
        }
        echo $this->_helper->json($Response);
    }


    /* public function getMenuToolAction()
     {
     $this->setNoRender();
     $this->disableLayout();
     $guidDAO = new Menu_Mediator();
     echo  $guidDAO->perform($this->getRequest()->getParam('guid'));
     }*/

    public function assignAction()
    {
        if (!$this->_request->isPost()) $this->_redirect('structure');

        //menuID    (type Params) - linkContainer
        
        $PMID          = $this->_request->getParam('pmid',0);
        $linkContainer = $this->_request->getParam('linkContainer');
        
        if (empty($PMID) || empty($linkContainer)) throw new Exception("Wrong menu assigning parameters");
        
        $Menu = new Menu ();
        
        $linkContainer = unserialize($linkContainer);
        
        $linkParams = $linkContainer['params'];
        $linkType   = $linkContainer['type']; 
        
         $updateData = array(
		        'Type' => $linkType,
    		    'Params' => serialize($linkParams),
        );

        $Menu->updateFullLink($PMID,$updateData);
        $this->_redirect('structure');
    }

    public function  getLinkAction()
    {
        $this->setNoRender();
        $this->disableLayout();
         
        $Response = array('errorCode'=>0, 'data'=>0);
        $ID = $this->getRequest()->getParam('id');
        $structureDAO = new Structure();
        $menuItem = $structureDAO->get($ID);
         
        if ($menuItem)
        {
            $Response['data'] = $menuItem;
        }
        else
        {
            $Response['data'] = 0;
            $Response['errorCode'] = 1;
            $Response['errorDescription'] = 'Row is not found in a DB.';
        }
         
        echo Zend_Json::encode($Response);
    }

    public function getSubSelectAction()
    {
        $this->disableLayout();
        $this->setNoRender();

        $Value = $this->_request->getParam ( 'linkType');

        $Data = array ();
        $Field = 'select';

        if ($Value == 'article') {
            Loader::includeModuleModels('page');
            $pageDAO = new  Page();
            
            $Pages = $pageDAO->getAll ();
            
            foreach ( $Pages as $Item ) {
                $articleMap = array('type' => 'simpleRoutedObject', 'params' => array('module'=>'page','controller'=>'index','action'=>'view','title'=>Transliteration::asURLSegment($Item['Title']),'id'=>$Item['ID'],'route'=>'articleFURL'));
                $Data [] = array ('linkDescriptor' => htmlentities(serialize($articleMap)) , 'title' => trim ( $Item ['Title'] ) );
            }
        }
        elseif ($Value == 'static')
        {
            $articleMap = array('module'=>'default','controller'=>'static','route'=>'default');
            $Pages = array('contact'=>'Контакты', 'map'=>'Карта', 'about'=>'О нас');

            foreach ( $Pages as $Key => $Item ) {
                 
                $articleMap['action'] = $Key;
                $linkParsms = array('type' => 'simpleRoutedObject', 'params' =>$articleMap); 
                $Data [] = array ('linkDescriptor' => htmlentities(serialize($linkParsms)) , 'title' => trim ( $Item ) );
            }
             
            $Data [] = array(
			   			'title' => "Форма заявки",
			'id'=> htmlentities(serialize(
            array ('module'=>'default',
                   'controller'=>'order',
				   'action'=>'index',
                   'route'=>'default')
                                        ) ) );

                                        
            $Data [] = array('title' => "Новости",'linkDescriptor'=> array('type' => 'nestedObject', 'params' => htmlentities(serialize(array ('module'=>'default','controller'=>'news','action'=>'index','route'=>'default')) ) ));
             
        }


        elseif ($Value == 'link') {
            $Field = "text";
        }



        elseif ($Value == 'gallery') {
            Loader::includeModuleModels('gallery');
            $galleryDAO = new Gallery();
            $galleries = $galleryDAO->getList();

            $Data [] = array ('id' => 'gallery', 'title' => 'Список альбомов' );
            foreach ( $galleries as $Item ) {

                $articleMap = array('module'=>'default','controller'=>'gallery','action'=>'view','id'=>$Item ['id'],'route'=>'gallery');
                 
                $Data [] = array ('linkDescriptor' => htmlentities(serialize($articleMap)) , 'title' => trim ( $Item ['Name'] ) );
            }
        }

        elseif ($Value == 'price') {

            $priceDAO = new Prices();
            $prices = $priceDAO->getList();

            foreach ( $prices as $Item ) {
                $articleMap = array('module'=>'default','controller'=>'price','action'=>'index','id'=>$Item ['id'],'route'=>'default');
                $Data [] = array ('linkDescriptor' => htmlentities(serialize($articleMap)) , 'title' => trim ( $Item ['Name'] ) );
            }
        }


        $result = array ('type' => 'success', 'data' => $Data, 'fieldType' => $Field );
        echo Zend_Json::encode( $result );
    }
    
    
    public function checkAction()
    {
        
    }

}



